<template>
  <div class="home">
    <header class="hero">
      <h1>知识卡片学习系统</h1>
      <p>将复杂文档转化为易于掌握的知识卡片，提高学习效率</p>
      <div class="cta-buttons" v-if="!userStore.isAuthenticated">
        <router-link to="/register" class="btn btn-primary">注册</router-link>
        <router-link to="/login" class="btn btn-secondary">登录</router-link>
      </div>
      <div class="cta-buttons" v-else>
        <router-link to="/learning-paths" class="btn btn-primary">开始学习</router-link>
        <router-link to="/documents" class="btn btn-secondary">管理文档</router-link>
      </div>
    </header>

    <!-- 已登录用户的学习引导区域 -->
    <section class="learning-guide" v-if="userStore.isAuthenticated">
      <h2>我的学习中心</h2>
      
      <div class="guide-cards">
        <!-- 学习路径卡片 -->
        <div class="guide-card" @click="navigateTo('/learning-paths')">
          <div class="card-icon">
            <div class="svg-icon path-icon"></div>
          </div>
          <div class="card-content">
            <h3>学习路径</h3>
            <p>系统化学习课程，按照最佳顺序掌握知识</p>
            <div class="card-footer">
              <span class="badge">{{ inProgressPaths }} 个进行中</span>
              <span class="arrow-icon">→</span>
            </div>
          </div>
        </div>
        
        <!-- 最近学习卡片 -->
        <div class="guide-card" @click="navigateTo('/progress')">
          <div class="card-icon">
            <div class="svg-icon recent-icon"></div>
          </div>
          <div class="card-content">
            <h3>继续学习</h3>
            <p>从上次离开的地方继续您的学习进度</p>
            <div class="card-footer" v-if="lastLearningPath">
              <span class="path-name">{{ lastLearningPath.title }}</span>
              <span class="arrow-icon">→</span>
            </div>
            <div class="card-footer" v-else>
              <span class="path-name">选择学习路径</span>
              <span class="arrow-icon">→</span>
            </div>
          </div>
        </div>
        
        <!-- 文档管理卡片 -->
        <div class="guide-card" @click="navigateTo('/documents')">
          <div class="card-icon">
            <div class="svg-icon document-icon"></div>
          </div>
          <div class="card-content">
            <h3>文档管理</h3>
            <p>上传新文档或管理现有文档和卡片</p>
            <div class="card-footer">
              <span class="badge">{{ documentCount }} 个文档</span>
              <span class="arrow-icon">→</span>
            </div>
          </div>
        </div>
        
        <!-- 学习报告卡片 -->
        <div class="guide-card" @click="navigateTo('/progress')">
          <div class="card-icon">
            <div class="svg-icon report-icon"></div>
          </div>
          <div class="card-content">
            <h3>学习报告</h3>
            <p>查看您的学习统计和掌握情况</p>
            <div class="card-footer">
              <span>查看报告</span>
              <span class="arrow-icon">→</span>
            </div>
          </div>
        </div>
      </div>
    </section>

    <section class="features">
      <h2>主要功能</h2>
      <div class="feature-grid">
        <div class="feature-card">
          <h3>文档解析</h3>
          <p>支持上传PDF、DOCX、TXT等格式文档，自动解析内容</p>
        </div>
        <div class="feature-card">
          <h3>知识卡片生成</h3>
          <p>智能分析文档内容，生成结构化的知识卡片</p>
        </div>
        <div class="feature-card">
          <h3>练习题生成</h3>
          <p>根据知识卡片内容，自动生成选择题、填空题和代码实践题</p>
        </div>
        <div class="feature-card">
          <h3>学习路径引导</h3>
          <p>系统化学习路径，引导您按最佳顺序掌握知识</p>
        </div>
      </div>
    </section>

    <section class="how-it-works">
      <h2>使用流程</h2>
      <div class="steps">
        <div class="step">
          <div class="step-number">1</div>
          <h3>上传文档</h3>
          <p>上传您需要学习的文档，支持多种格式</p>
        </div>
        <div class="step">
          <div class="step-number">2</div>
          <h3>生成学习路径</h3>
          <p>系统自动分析文档内容，创建结构化的学习路径</p>
        </div>
        <div class="step">
          <div class="step-number">3</div>
          <h3>按序学习</h3>
          <p>按照推荐顺序学习知识卡片，完成练习题巩固</p>
        </div>
        <div class="step">
          <div class="step-number">4</div>
          <h3>跟踪进度</h3>
          <p>系统自动保存学习进度，随时可以继续学习</p>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/store/user';
import { learningPathApi } from '@/services/api';
import { documentApi } from '@/services/api';

const router = useRouter();
const userStore = useUserStore();

// 状态变量
const inProgressPaths = ref(0);
const documentCount = ref(0);
const lastLearningPath = ref(null);

// 方法
const navigateTo = (path) => {
  router.push(path);
};

// 获取用户学习路径数据
const fetchUserData = async () => {
  if (!userStore.isAuthenticated) return;
  
  try {
    // 获取学习路径
    const pathsResponse = await learningPathApi.getUserPaths();
    const paths = pathsResponse.data;
    
    // 计算进行中的路径数量
    inProgressPaths.value = paths.filter(path => 
      path.userProgress && path.userProgress.status === 'in_progress'
    ).length;
    
    // 获取最近的学习路径
    if (paths.length > 0) {
      // 按最后访问时间排序
      const sortedPaths = [...paths].sort((a, b) => {
        const aDate = a.userProgress?.lastAccessedAt ? new Date(a.userProgress.lastAccessedAt) : new Date(0);
        const bDate = b.userProgress?.lastAccessedAt ? new Date(b.userProgress.lastAccessedAt) : new Date(0);
        return bDate - aDate;
      });
      
      lastLearningPath.value = sortedPaths[0];
    }
    
    // 获取文档数量
    const docsResponse = await documentApi.getAll();
    documentCount.value = docsResponse.data.length;
  } catch (err) {
    console.error('获取用户数据失败', err);
  }
};

onMounted(() => {
  fetchUserData();
});
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.hero {
  text-align: center;
  padding: 4rem 0;
}

.hero h1 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.hero p {
  font-size: 1.2rem;
  margin-bottom: 2rem;
  color: #666;
}

.cta-buttons {
  display: flex;
  justify-content: center;
  gap: 1rem;
}

.btn {
  padding: 0.75rem 1.5rem;
  border-radius: 4px;
  font-weight: bold;
  text-decoration: none;
  transition: all 0.3s ease;
}

.btn-primary {
  background-color: #4CAF50;
  color: white;
}

.btn-primary:hover {
  background-color: #45a049;
}

.btn-secondary {
  background-color: #f1f1f1;
  color: #333;
}

.btn-secondary:hover {
  background-color: #e1e1e1;
}

/* 学习引导区域 */
.learning-guide {
  margin: 2rem 0 4rem;
}

.guide-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
  margin-top: 2rem;
}

.guide-card {
  display: flex;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  cursor: pointer;
  padding: 1.5rem;
}

.guide-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
}

.card-icon {
  flex-shrink: 0;
  width: 60px;
  height: 60px;
  margin-right: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f8f9fa;
  border-radius: 12px;
}

.svg-icon {
  width: 32px;
  height: 32px;
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}

/* 内联SVG图标 */
.path-icon {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%234CAF50'%3E%3Cpath d='M12 2L4.5 20.29l.71.71L12 18l6.79 3 .71-.71z'/%3E%3C/svg%3E");
}

.recent-icon {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%233498db'%3E%3Cpath d='M12 2C6.5 2 2 6.5 2 12s4.5 10 10 10 10-4.5 10-10S17.5 2 12 2zm0 18c-4.4 0-8-3.6-8-8s3.6-8 8-8 8 3.6 8 8-3.6 8-8 8zm-1-13h2v6h-2zm0 8h2v2h-2z'/%3E%3C/svg%3E");
}

.document-icon {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23f39c12'%3E%3Cpath d='M14 2H6c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V8l-6-6zm-1 7V3.5L18.5 9H13z'/%3E%3C/svg%3E");
}

.report-icon {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%239b59b6'%3E%3Cpath d='M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z'/%3E%3C/svg%3E");
}

.card-content {
  flex: 1;
}

.card-content h3 {
  margin: 0 0 0.5rem;
  font-size: 1.2rem;
  color: #333;
}

.card-content p {
  margin: 0 0 1rem;
  font-size: 0.9rem;
  color: #666;
  line-height: 1.4;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
}

.badge {
  background-color: #e8f5e9;
  color: #4CAF50;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

.path-name {
  color: #3498db;
  font-weight: 500;
}

.arrow-icon {
  font-size: 1.2rem;
  color: #4CAF50;
}

.features, .how-it-works {
  margin: 4rem 0;
}

h2 {
  text-align: center;
  margin-bottom: 2rem;
  font-size: 2rem;
}

.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 2rem;
}

.feature-card {
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-card h3 {
  margin-bottom: 0.75rem;
  color: #333;
}

.steps {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 2rem;
  text-align: center;
}

.step {
  position: relative;
  padding: 1.5rem;
}

.step-number {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #4CAF50;
  color: white;
  font-weight: bold;
  margin: 0 auto 1rem;
}

.step h3 {
  margin-bottom: 0.75rem;
}

@media (max-width: 768px) {
  .hero h1 {
    font-size: 2rem;
  }
  
  .hero p {
    font-size: 1rem;
  }
  
  .feature-grid, .steps {
    grid-template-columns: 1fr;
  }
  
  .guide-cards {
    grid-template-columns: 1fr;
  }
}
</style> 